//
//  LoginViewModel.m
//  MVVMwithRACDemo
//
//  Created by Darsky on 2020/3/31.
//  Copyright © 2020 Darsky. All rights reserved.
//

#import "LoginViewModel.h"

@interface LoginViewModel () {
    ASImageNode        *_loginLogoNode;
    
    ASTextNode         *_loginTitleNode;
    
    ASDisplayNode      *_loginBGNode;
        
    ASEditableTextNode *_accountTextNode;
        
    ASEditableTextNode *_passwordTextNode;
    
    ASButtonNode       *_loginButtonNode;
}

@end

@implementation LoginViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _loginBGNode = [[ASDisplayNode alloc] init];
        _loginBGNode.backgroundColor = [UIColor colorWithRed:0/255.0
                                                       green:163/255.0
                                                        blue:217/255.0
                                                       alpha:1.0];
        [self addSubnode:_loginBGNode];
        
        //登录图标
        _loginLogoNode = [[ASImageNode alloc] init];
        _loginLogoNode.image = [UIImage imageNamed:@"img_public_avatar_default"];
        _loginLogoNode.style.preferredSize = CGSizeMake(62, 62);
        [self addSubnode:_loginLogoNode];
        
        //登录标题
        _loginTitleNode = [[ASTextNode alloc] init];
        _loginTitleNode.style.preferredSize = CGSizeMake(40, 30);
        _loginTitleNode.attributedText = [[NSAttributedString alloc]
                                          initWithString:@"登录"
                                          attributes:@{
                                              NSFontAttributeName:[UIFont systemFontOfSize:18],
                                              NSForegroundColorAttributeName:[UIColor blackColor]}];
        [self addSubnode:_loginTitleNode];
        
        //用户名输入框
        _accountTextNode = [[ASEditableTextNode alloc] init];
        _accountTextNode.attributedPlaceholderText = [[NSAttributedString alloc]
                                                      initWithString:@"请输入用户名"
                                                      attributes:@{
                                                          NSFontAttributeName:[UIFont systemFontOfSize:15],
                                                          NSForegroundColorAttributeName:[UIColor
                                                                                          colorWithRed:153/255.0
                                                                                          green:153/255.0
                                                                                          blue:153/255.0
                                                                                          alpha:1.0]}];
        _accountTextNode.attributedText = [[NSAttributedString alloc]
                                           initWithString:@""
                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        _accountTextNode.backgroundColor = [UIColor redColor];
        _accountTextNode.maximumLinesToDisplay = 1;
        [self addSubnode:_accountTextNode];
        

        
        //密码输入框
        _passwordTextNode = [[ASEditableTextNode alloc] init];
        _passwordTextNode.secureTextEntry = YES;
        _passwordTextNode.maximumLinesToDisplay = 1;
        _passwordTextNode.attributedPlaceholderText = [[NSAttributedString alloc]
                                                      initWithString:@"请输入密码"
                                                      attributes:@{
                                                          NSFontAttributeName:[UIFont systemFontOfSize:15],
                                                          NSForegroundColorAttributeName:[UIColor
                                                                                          colorWithRed:153/255.0
                                                                                          green:153/255.0
                                                                                          blue:153/255.0
                                                                                          alpha:1.0]}];
        _passwordTextNode.attributedText = [[NSAttributedString alloc]
                                           initWithString:@""
                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        _passwordTextNode.backgroundColor = [UIColor greenColor];
        [self addSubnode:_passwordTextNode];
        
        _loginButtonNode = [[ASButtonNode alloc] init];
        [_loginButtonNode setAttributedTitle:[[NSAttributedString alloc] initWithString:@"立即登录" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}] forState:UIControlStateNormal];
        _loginButtonNode.contentEdgeInsets = UIEdgeInsetsMake(6, 10, 6, 10);
        _loginButtonNode.clipsToBounds = YES;
        _loginButtonNode.cornerRadius  = 13;
        _loginButtonNode.borderWidth = 0.7;
        _loginButtonNode.borderColor = [UIColor colorWithRed:153/255.0
                                                  green:153/255.0
                                                   blue:153/255.0
                                                  alpha:1.0].CGColor;
        [self addSubnode:_loginButtonNode];
//        [_loginButtonNode addTarget:self
//                             action:@selector(didHistoryButtonTouch:)
//              forControlEvents:ASControlNodeEventTouchUpInside];
//        [_titleNodes addObject:historyNode];
    }
    return self;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    
    _accountTextNode.style.flexGrow = 1;
    _passwordTextNode.style.flexGrow = 1;
    _passwordTextNode.secureTextEntry = YES;
    _loginButtonNode.style.preferredSize = CGSizeMake(120, 40);
    //上部布局
    ASInsetLayoutSpec *loginLogoSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(40, 0, 10, 0) child:_loginLogoNode];
    ASStackLayoutSpec *topContentSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
                                                                                spacing:10
                                                                         justifyContent:ASStackLayoutJustifyContentStart
                                                                             alignItems:ASStackLayoutAlignItemsCenter
                                                                               children:@[loginLogoSpec,_loginTitleNode]];
    
    ASBackgroundLayoutSpec *topSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:topContentSpec
                                                                                 background:_loginBGNode];
    //中部布局
    ASStackLayoutSpec *middleSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[_accountTextNode,_passwordTextNode]];
    ASStackLayoutSpec *bottomSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentCenter alignItems:ASStackLayoutAlignItemsCenter children:@[_loginButtonNode]];
    bottomSpec.lineSpacing = 10;
    ASStackLayoutSpec *contextSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10.0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[topSpec,middleSpec,bottomSpec]];
  
    return contextSpec;
}


@end
